package com.bto.solar.work.service.biz_stage_handler;

import com.bto.solar.work.common.BizException;
import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskEnum;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.constant.Constants;
import com.bto.solar.work.dao.ICustomerService;
import com.bto.solar.work.dao.IOrderBaseService;
import com.bto.solar.work.entity.OrderBase;
import com.bto.solar.work.lease.LeaseApi;
import com.bto.solar.work.service.BtoOrderService;
import com.bto.solar.work.service.BtoOrderStateService;
import com.bto.solar.work.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 王小波
 * @description:  预审
 * @date 2023/7/24 14:48
 */
@Slf4j
@Component
public class YuShenStageHandler implements StageHandler {
    @Autowired
    private IOrderBaseService orderService;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private BtoOrderService btoOrderService;
    @Autowired
    private BtoOrderStateService btoOrderStateService;
    @Autowired
    private YeZhuStageHandler yeZhuStageHandler;
    @Autowired
    private KanChaStageHandler kanChaStageHandler;
    @Autowired
    private LeaseApi leaseApi;

    @Override
    public void createNewInitState(String orderId, String stageId, String taskId) {
        btoOrderStateService.createOrderState(orderId, BizStageEnum.PRE_APPROVAL.getStageId(), BizTaskEnum.TASK_YSXX.getTaskId(), BizTaskStateEnum.WAITING_FILLED.getStateId());
    }


    /**
    *  语预审信息，直接提交给越秀审核
    */
    @Override
    public void  updateToWaitingApprovalState(String orderId, String stageId, String taskId) {
        checkOrderCanPreApproval(orderId);
        //推送越秀预审信息
        OrderBase orderBase = orderService.getById(orderId);
        if (orderBase == null) {
            throw new BizException("订单{}不存在, 无法进行资料推送！", orderId);
        }
        //预审信息推送
        val pushed = leaseApi.pushCustomerPreApproval(orderBase);
        if(!pushed){
            throw new BizException("推送越秀，承租人预审，推送预审信息失败 订单{} , 请重新操作" , orderId);
        }
        //使用协议推送
        val pushFileed = leaseApi.isFileAndContactSyncPush(orderBase, Constants.YSXX_FILE_LIST, false);
        if(!pushFileed){
            throw new BizException("推送越秀，承租人预审，推送预审图片失败 订单{} , 请重新操作" , orderId);
        }
        btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.PRE_APPROVAL.getStageId(), BizTaskEnum.TASK_YSXX.getTaskId(),
                BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId() ,
                BizTaskStateEnum.WAITING_FILLED.getStateId() ,true);
    }

    @Transactional
    @Override
    public void updateToApprovalPassState(String orderId, String stageId, String taskId , String approvalType) {
        btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.PRE_APPROVAL.getStageId(), BizTaskEnum.TASK_YSXX.getTaskId(),
                BizTaskStateEnum.APPROVAL_PASS_LEASE.getStateId() ,
                BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId() ,true);

        //开启业主信息和踏勘阶段
        yeZhuStageHandler.createNewInitState(orderId , BizStageEnum.CUSTOMER_INFO.getStageId(), null);
        kanChaStageHandler.createNewInitState(orderId , BizStageEnum.KAN_CHA.getStageId(), BizTaskEnum.TASK_TA_KAN.getTaskId());
    }

    @Override
    public void updateToApprovalRejectState(String orderId, String stageId, String taskId , String approvalType  , Object rejectInfo) {
        btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.PRE_APPROVAL.getStageId(), BizTaskEnum.TASK_YSXX.getTaskId(),
                BizTaskStateEnum.TERMINATE.getStateId() ,
                BizTaskStateEnum.WAITING_APPROVAL_LEASE.getStateId() ,true);
    }


    /**
    *  检查订单是否可以提交预审
    */
    private void checkOrderCanPreApproval(String orderId){
        OrderBase orderBase = orderService.getById(orderId);
        String fddSignTaskId = orderBase.getFddSignTaskId();
        if(StringUtils.isEmpty(fddSignTaskId) || "0".equals(fddSignTaskId) || "1".equals(fddSignTaskId)){
            throw new BizException("订单 {} ，信息使用授权书 未签约，无法提交预审" , orderId);
        }
    }
}
